import 'package:f200/common/index.dart';
import 'package:f200/features/cart/application/controller/shopping_screen_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../domain/cart_model.dart';

class ShoppingCartScreen extends StatefulWidget {
  const ShoppingCartScreen({super.key});

  @override
  State<ShoppingCartScreen> createState() => _ShoppingCartScreenState();
}

class _ShoppingCartScreenState extends State<ShoppingCartScreen>
    with AutomaticKeepAliveClientMixin {
  var controller = Get.find<ShoppingScreenController>();

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return SafeArea(
      child: Column(
        children: [
          Expanded(
            child: _buildBody(context, controller.cart.value),
          ),
          _buildBottomBar(controller.cart.value),
        ],
      ),
    );
  }

  Widget _buildBody(BuildContext context, CartModel cart) {
    return Container(
      color: ShoppingScreenController.backgroundColor,
      padding: EdgeInsets.symmetric(horizontal: 4),
      child: Column(
        children: [
          _buildHeader(),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(bottom: 2),
              color: Color.fromARGB(255, 238, 237, 236),
              child: SingleChildScrollView(
                child: Container(),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Column(
      children: [
        //购物车标题，位置，管理
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: [
              Text(
                '购物车',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              //定位的位置
              Expanded(
                child: Row(
                  children: [
                    SizedBox(width: 6),
                    Icon(
                      Icons.gps_fixed,
                      size: 14,
                      color: Colors.grey,
                    ),
                    InkWell(
                      child: Text(
                        '中方县泸阳勇发榨油厂',
                        style: TextStyle(
                          fontSize: 12,
                          color: Colors.grey,
                        ),
                      ),
                    ),
                    Icon(
                      Icons.keyboard_arrow_right,
                      size: 16,
                      color: Colors.grey,
                    ),
                  ],
                ),
              ),
              TextButton(
                  onPressed: () {
                    // ToastUtils.error('提示您注意。。。');
                  },
                  child: Text('管理')),
            ],
          ),
        ),
        //全部 收藏
        Row(
          children: [
            InkWell(
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                margin: EdgeInsets.symmetric(vertical: 6),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(16)),
                ),
                child: Row(
                  children: [
                    Text('全部 (${controller.totalNum.value})',
                        style: TextStyle(fontSize: 12)),
                    SizedBox(width: 2),
                    Icon(Icons.keyboard_arrow_down,
                        color: Colors.grey, size: 16),
                  ],
                ),
              ),
            ),
            SizedBox(width: 12),
            InkWell(
              child: Container(
                padding: EdgeInsets.fromLTRB(10, 4, 10, 4),
                margin: EdgeInsets.symmetric(vertical: 6),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(16)),
                ),
                child: Row(
                  children: [
                    Icon(
                      Icons.favorite,
                      size: 12,
                      color: Colors.amber,
                    ),
                    SizedBox(width: 2),
                    Text('收藏', style: TextStyle(fontSize: 12)),
                    SizedBox(width: 2),
                    Icon(Icons.keyboard_arrow_right,
                        color: Colors.grey, size: 16),
                  ],
                ),
              ),
            )
          ],
        ),
      ],
    );
  }

  ///底部结算
  Container _buildBottomBar(CartModel? cart) {
    logD('======================_buildBottomBar');
    if (cart == null) return Container();
    logD('======================_buildBottomBar ok----------');

    return Container(
      height: 44,
      decoration: BoxDecoration(
        border: Border(top: BorderSide(color: Color(0xFFededed), width: .3)),
      ),
      child: Row(
        children: [
          SizedBox(width: 16),
          Expanded(
            child: CmCheckbox(
              value: cart.allIsSelected,
              onChanged: (v) => controller.selectAll(v),
              spacing: 6,
              descriptionWidget: Text(
                '全选',
                style: TextStyle(
                    color: Theme.of(context).primaryColor, fontSize: 12),
              ),
            ),
          ),
          controller.totalAmount.value == 0
              ? Container()
              : Text(
                  '已包邮',
                  style: TextStyle(color: Color(0xFF666666), fontSize: 9),
                ),
          SizedBox(width: 4),
          Text('合计：', style: TextStyle(fontSize: 12)),
          SizedBox(width: 8),
          Text(
            '￥',
            style: TextStyle(fontSize: 10, color: Color(0xFFff5410)),
          ),
          Text(
            '${CommonUtils.removeDecimalZeroFormat(controller.totalAmount.value)}',
            style: TextStyle(fontSize: 14, color: Color(0xFFff5410)),
          ),
          SizedBox(width: 8),
          Container(
            color: const Color.fromARGB(255, 232, 44, 35),
            height: 36,
            width: 100,
            child: InkWell(
              onTap: () {
                // Loading.show('结算...');
              },
              splashColor: const Color.fromARGB(255, 240, 118, 111),
              borderRadius: BorderRadius.all(Radius.circular(18)),
              child: Container(
                height: 36,
                width: 100,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  // gradient: _mainGradient,
                  borderRadius: BorderRadius.all(Radius.circular(18)),
                ),
                child: Text(
                  '结算(${controller.settlementCount.value})',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
          SizedBox(width: 8),
        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
